import 'package:dio/dio.dart';
import 'package:flutter_app/http/Skyerror.dart';
import 'package:flutter_app/http/request/BaseRequest.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
const userDio = true;

class SkyHttp{
  SkyHttp._();
  static SkyHttp _instance;
  static SkyHttp getInstance(){
    if(_instance == null){
      _instance = SkyHttp._();
    }
    return _instance;
  }
  Future fire(BaseRequest request) async{
    if(request.httpMethod() == HttpMethod.GET){
        return _doGet(request);
    }else{

    }

  }

  Future _doGet(BaseRequest request) async{
    var uri ;
    if(request.params.length !=0){
      uri = Uri.https(request.url(), request.path(),request.params);
    }else{
      uri = Uri.https(request.url(), request.path());
    }
    var response;
    var header = getHeaderParams();
    var result ;
    if(userDio){
      response = await Dio().get(uri.toString(),options: Options(headers: header));
      result = response.data;
    }else{
      response = await http.get(uri.toString(),headers: header);
      Utf8Decoder utf8decoder = Utf8Decoder();
      if(response.headers["content-type"].contains("/json")){
        result = json.decode(utf8decoder.convert(response.bodyBytes));
      }else{
        result = utf8.decode(response.bodyBytes);
      }
    }
    if(response.statusCode == 200){
      //服务端自我定义
      if(result["code"] == 0){
        return result["data"];
      }else{
        throw Exception(result["msg"]);
      }
    }else if (response.statusCode == 401){
      throw Exception(NeedLogin());
    }else if(response.statusCode == 403){
      throw Exception(NeedAuth());
    }else{
      throw Exception('statusCode:${response.statusCode},message:${result.toString()}');
    }
  }
  getHeaderParams(){
    return {"auth-token":"123123"};
  }
}